package com.dongjiantong.quarantine.model;

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 
 */
@Table(name="djt_butcher_reports")
@Data
public class DjtButcherReports extends BaseEntity implements Serializable {

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();


    /**
     * 申报编号
     */
    private String number;

    /**
     * 申报时间
     */
    private Date date;

    /**
     * 入场监督检疫ID
     */
    private Long admission_inspection_id;

    /**
     * 入场监督检疫编号
     */
    private String admission_inspection_number;

    /**
     * 屠宰场id
     */
    private Long butcher_farm_id;

    /**
     * 屠宰场名称
     */
    private String butcher_farm_name;

    /**
     * 申报人ID
     */
    private Long user_id;

    /**
     * 申报人姓名
     */
    @NotEmpty
    private String user_name;

    /**
     * 申报人电话
     */
    private String user_mobile;

    /**
     * 状态 -2:不受理 -1：已处理 0：未提交 1：待处理 2：宰前检查 3：同步检疫 4：合格发证 ：5 发证
     */
    private Integer status;

    /**
     * 检疫合格证编号
     */
    private String quarantine_cert_number;

    /**
     * 检疫合格证照片
     */
    @Transient
    private List<String> quarantine_cert_photo_list;

    /**
     * 产品类型：牛、马、猪...
     */
    private Integer animal_type;

    /**
     * 单位, 只、件
     */
    private Integer unit;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 禽兽耳标号
     */
    @Transient
    private List<String> ear_tag_numbers;

    /**
     * 货主
     */
    private String owner;

    /**
     * 来源id
     */
    private Long source_id;

    /**
     * 来源名称
     */
    private String source_name;

    /**
     * 是否本地
     */
    private Integer is_local;

    /**
     * 受理人id
     */
    private Long survey_user_id;

    /**
     * 受理人名称
     */
    private String survey_user_name;

    /**
     * 受理人电话
     */
    private String survey_user_mobile;

    /**
     * 处理意见
     */
    private String handle_opinion;

    /**
     * 宰前合格数
     */
    private Integer before_slaughter_qualified_quantity;

    /**
     * 宰前不合格数
     */
    private Integer before_slaughter_unqualified_quantity;

    /**
     * 宰前检查结果
     */
    private String before_slaughter_result;

    /**
     * 宰前检查无害化ID
     */
    private Long before_slaughter_harmless_id;

    /**
     * 在前检查无害化编号
     */
    private String before_slaughter_harmless_number;

    /**
     * 检疫人id
     */
    private Long vet_user_id;

    /**
     * 检疫人名称
     */
    private String vet_user_name;

    /**
     * 检疫人电话
     */
    private String vet_user_mobile;

    /**
     * 旋毛虫镜检数量
     */
    private Float trichina_exam_quantity;

    /**
     * 同步检疫合格数
     */
    private Float sync_qualified_quantity;

    private Float sync_unqualified_quantity;

    /**
     * 同步检疫无害化ID
     */
    private Long sync_slaughter_harmless_id;

    /**
     * 同步检疫无害化编号
     */
    private String sync_slaughter_harmless_number;

    /**
     * 头部检疫、       酮体检疫、    内脏检疫
     * headerNum : 0 ;ketoneBody : 0 ;internal : 0
     */
    @Transient
    private Map<String , Double> extra;

    /**
     * 扩展信息 头部检疫、酮体检疫、内脏检疫
     */
    @JsonIgnore
    private String extra_json;

    /**
     * 1：正常，-1：删除
     */
    private Integer delete_flag;

    @NotEmpty
    private Date created_at;

    @NotEmpty
    private Date updated_at;

    private static final long serialVersionUID = 1L;


    public void setExtra_json(String extra_json){
        this.extra_json = extra_json;
        if(Strings.isNullOrEmpty(extra_json)){
            this.extra = Maps.newHashMap();
        }else {
            try {
                this.extra = objectMapper.readValue(extra_json, new TypeReference<Map<String ,Double>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setExtra(Map<String , Double> extra) {
        this.extra = extra;

        if(extra == null || extra.isEmpty()){
            this.extra_json = null;
        }else {
            try {
                this.extra_json = objectMapper.writeValueAsString(extra);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }
}